package cn.wyu.dao;

import cn.wyu.pojo.Article;
import cn.wyu.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

public class ArticleDaoimpl implements ActicleDao {
    @Override
    public List<Article> findByArticleType(String type) {

        //1、用jdbc查询数据库
        String sql = "select * from ec_article where type_code like ?";
        ResultSet resultSet = JDBCUtil.executeQuery(sql, type + "%");


        //2、将数据封装到Acticle对象中

        List<Article> list = new LinkedList<>();
        try {
            while (resultSet.next()) {
                Article article = new Article();
                int id = resultSet.getInt("ID");
                String type_code = resultSet.getString("type_code");
                String title = resultSet.getString("TITLE");
                String supplier = resultSet.getString("supplier");
                String price = resultSet.getString("price");
                String discount = resultSet.getString("discount");
                String locality = resultSet.getString("locality");
                String putaway_date = resultSet.getString("putaway_Date");
                int Storage = resultSet.getInt("storage");
                String image = resultSet.getString("image");
                String description = resultSet.getString("description");
                String create_date = resultSet.getString("create_Date");
                int disabled = resultSet.getInt("disabled");
                article.setId(id);
                article.setTypeCode(type_code);
                article.setTitle(title);
                article.setSupplier(supplier);
                article.setPrice(Double.valueOf(price));

                DecimalFormat format = new DecimalFormat("0.00");
                double aDouble = Double.valueOf(discount);
                double dprice = Double.valueOf(price);
                article.setDiscount(Double.valueOf(format.format(aDouble * dprice)));
                article.setLocality(locality);
                article.setPutawayDate(putaway_date);
                article.setStorage(Storage);
                article.setImage(image);
                article.setDescription(description);
                article.setCreateDate(create_date);
                article.setDisabled(disabled);
//                System.out.println("come to dao 层");
//                System.out.println(article);
                list.add(article);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public String findArticleType(String typeCode) {
        System.out.println("查询商品类型");
        String sql = "SELECT * from ec_article_type WHERE code = ?";
        ResultSet resultSet = JDBCUtil.executeQuery(sql, typeCode);
        String articleType = null;
        try {
            while (resultSet.next())
                articleType = resultSet.getString("NAME");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        System.out.println("articleName:" + articleType);
        return articleType;
    }
}
